$(function () {
    var ID = 0
    $("#jqGrid").jqGrid({
        url: baseURL + 'dic/user/list',
        datatype: "json",
        colModel: [
            { label: 'key', name: 'key',  width: 45, key: true },
            { label: 'value', name: 'value', width: 75},
            { label: '描述信息', name: 'desc', sortable: false, width: 75 }
        ],
        viewrecords: true,
        height: 500,
        rowNum: 100,
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        //pager: "#jqGridPager",
        jsonReader : {
            root: "list",
            page: "currPage",
            total: "totalPage",
            records: "totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            username: null
        },
        showList: true,
        title:null,
        roleList:{},
        user:{
            key:'',
            value:'',
            desc:''
        },
        flag:true,
        setDisabled:true,
        isDisabled:''

    },
    directives:{
        focus:{
            update: function (el) {
                try {
                    var input = el
                    if(!input.value){
                        return false;
                    }
                    input.value = input.value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\w+$]/g, "");
                    input.dispatchEvent(new Event(modifiers.lazy ? 'change' : 'input'))
                } catch (e) {
                }
            }
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.flag = true;
            vm.showList = false;
            vm.isDisabled = true
            $('.isDisabled').removeAttr("disabled")
            vm.title = "新增";
            vm.user = {
                key:'',
                value: '',
                desc: ''
            }
        },

        update: function () {
            vm.flag = false;

            var rowKey = $("#jqGrid").getGridParam("selarrrow");
            console.log(rowKey)
            if(rowKey.length>1){
                alert("只能选择一条记录");
                return false
            }else if(rowKey.length==0){
                alert("请选择一条记录");
                return false
            }
            vm.showList = false;
            $('.isDisabled').attr("disabled",'disabled')
            vm.getElementValue()
            vm.title = "修改";

        },
        del: function () {
            var rowKey = $("#jqGrid").getGridParam("selarrrow");
            console.log(rowKey)
            if(rowKey.length==0){
                alert("请选择一条记录");
                return false
            }
            // JSON.stringify
            vm.getElementValue()
            // if(rowKey.length>1){
            //     parm=rowKey
            // }else {
            //     parm=vm.user
            // }
            // console.log('sss',parm)
            // return false;
                confirm('确定要删除选中的记录？', function(){

                httpRequest("dic/user/delete","POST",rowKey.join(",")).done(function (data) {

                    alert('操作成功', function(){
                        vm.reload();
                    });

                })
            });
        },
        saveOrUpdate: function () {
            var url = vm.flag ? "dic/user/save" : "dic/user/updata";

            httpRequest(url,"POST",JSON.stringify(vm.user)).done(function (data) {
                if(data.code === 0){
                    alert('操作成功', function(){
                        vm.reload();
                    });
                }else {
                    alert(data.msg)
                }

            })
        },
        getUser: function(userId){
            $.get(baseURL + "sys/user/info/"+userId, function(r){
                vm.user = r.user;
                vm.user.password = null;

            });
        },
        getElementValue: function(){
            var jqGrid_key = $(".success td[aria-describedby$='jqGrid_key']").text()
            var jqGrid_value = $(".success td[aria-describedby$='jqGrid_value']").text()
            var jqGrid_desc = $(".success td[aria-describedby$='jqGrid_desc']").text()
            console.log(jqGrid_desc)
            vm.user.key = jqGrid_key;
            vm.user.value = jqGrid_value;
            vm.user.desc = jqGrid_desc;

        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.username},
                page:page
            }).trigger("reloadGrid");
        }
    }

});
